package com.bacon.gmall.order.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author： bacon、
 * @version： 1.0
 * @create： 2021-01-11 13:11
 * @description:
 */
@Configuration
public class OrderDeadLetterMQConfig {
    /**
     * 取消订单，发送延迟队列
     */
    /**
     * 取消订单，发送延迟队列
     */
    public static final String EXCHANGE_DIRECT_ORDER_CANCEL = "exchange.direct.order.cancel";//"exchange.direct.order.create" test_exchange;
    public static final String ROUTING_ORDER_CANCEL1 = "order.create1";
    public static final String ROUTING_ORDER_CANCEL2 = "order.create2";
    //延迟取消订单队列
    public static final String QUEUE_ORDER_CANCEL1 = "queue.order.cancel1";
    public static final String QUEUE_ORDER_CANCEL2 = "queue.order.cancel2";
    //取消订单 延迟时间 单位：毫秒
    public static final int DELAY_TIME = 2 * 60 * 60 * 1000;//全局设置2小时

    //TTL的队列
    @Bean
    public Queue queueOrderCancel1() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", EXCHANGE_DIRECT_ORDER_CANCEL);
        arguments.put("x-dead-letter-routing-key", ROUTING_ORDER_CANCEL2);
        arguments.put("x-message-ttl", DELAY_TIME);//单位是毫秒
        return QueueBuilder.durable(QUEUE_ORDER_CANCEL1).withArguments(arguments).build();
    }

    //普通队列
    @Bean
    public Queue queueOrderCancel2() {
        return QueueBuilder.durable(QUEUE_ORDER_CANCEL2).build();
    }

    //交换机 （普通）
    @Bean
    public Exchange exchangeOrderCancel() {
        return ExchangeBuilder.directExchange(EXCHANGE_DIRECT_ORDER_CANCEL).build();
    }

    //绑定 2遍
    @Bean
    public Binding bindingOrderCancel1(
            @Qualifier(value = "queueOrderCancel1") Queue qc1,
            @Qualifier(value = "exchangeOrderCancel") Exchange eoc) {
        return BindingBuilder.bind(qc1).to(eoc).with(ROUTING_ORDER_CANCEL1).noargs();
    }

    @Bean
    public Binding bindingOrderCancel2(
            @Qualifier(value = "queueOrderCancel2") Queue qc2,
            @Qualifier(value = "exchangeOrderCancel") Exchange eoc) {
        return BindingBuilder.bind(qc2).to(eoc).with(ROUTING_ORDER_CANCEL2).noargs();
    }


}
